<template>
    <div>
        <div style="margin-top: 20px;margin-bottom: 20px">
            <van-icon name="arrow-left" size="0.33rem" @click="return_home" style="float: left;margin-left: 20px"></van-icon>
            <span style="font-size: 20px;font-family: 楷体;margin-left: 130px">完善信息</span>
        </div>
        <hr>
        <div align="center" style="margin-top: 30px" v-if="uploadStatus == 0">
            <el-form status-icon :model="form" :rules="rules" class="demo-ruleForm" ref="form" style="width: 300px">
                <el-form-item label="真实姓名" prop="clientName">
                    <el-input v-model="form.clientName"></el-input>
                </el-form-item>
                <el-form-item label="身份证号" prop="cardNumber">
                    <el-input v-model="form.cardNumber"></el-input>
                </el-form-item>
                <!-- 身份证照片 -->
                <el-form-item label="身份证照片">
                    <el-upload
                            class="avatar-uploader"
                            action="http://192.168.0.100:81/clms-client/client/upload"
                            :headers= "headers"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submit('form')" >提交</el-button>
                    <el-button>重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div align="center" style="margin-top: 30px" v-if="uploadStatus == 1">
            <h1 style="color: red;font-size: 20px">您的信息已经完善</h1>
            <el-form status-icon  class="demo-ruleForm" style="width: 300px;margin-top: 20px">
                <el-form-item label="真实姓名">
                    <el-input v-model="this.$store.state.user.clientName" readonly="readonly"></el-input>
                </el-form-item>
                <el-form-item label="身份证号" >
                    <el-input v-model="this.$store.state.user.cardNumber" readonly="readonly"></el-input>
                </el-form-item>
            </el-form>
        </div>
    </div>

</template>

<script>
    import {uploadData} from '../../request/userinfo/userinfo';
    export default {
        name: "",
        data() {
            return {
                form: {
                    id: this.$store.state.user.id,
                    clientName: '',
                    cardNumber: '',
                    frontPhoto: ''
                },
                rules: {
                    clientName: [
                        { required: true, message: '请输入真实姓名', trigger: 'blur' },
                        { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
                    ],
                    cardNumber: [
                        { required: true, message: '请输入您的身份证号', trigger: 'blur' },
                        { min: 18, max: 18, message: '请输入您的身份证号18位', trigger: 'blur' }
                    ]
                },
                headers: {
                    'Token' : sessionStorage.getItem("token")
                },
                imageUrl: '',
                uploadStatus: this.$store.state.user.uploadStatus
            }
        },
        methods: {
            handleAvatarSuccess(res) {
                this.imageUrl = res;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            submit(formName) {
                console.log(this.imageUrl);
                this.form.frontPhoto = this.imageUrl;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if ( this.imageUrl == "") {
                            this.$toast.fail("您还没有上传身份证照片")
                        } else {
                            uploadData(this.form).then( result => {
                                if (result.code ==200) {
                                    this.$toast.success("完善信息成功");
                                    user().then( result => {
                                        console.log(this.$store.state.user);
                                        this.$store.state.user = result.object
                                    })
                                } else {
                                    this.$toast.fail(result.object)
                                }
                            })
                        }
                    } else {
                        this.$toast.fail("错误")
                    }
                });
            },
            return_home() {
                this.$router.go(-1);
            },
        }
    }
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 100px;
        height: 100px;
        line-height: 100px;
        text-align: center;
    }
    .avatar {
        width: 100px;
        height: 100px;
        display: block;
    }
</style>
